package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class MyLinearLayout extends ViewGroup {
    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        int measureWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeightSize = MeasureSpec.getSize(heightMeasureSpec);

        Log.i("MyLinearLayout", "onMeasure: widthMode = " + measureWidthMode
            + " heightMode= " + measureHeightMode + " widthSize= " + measureWidthSize + " heightSize= " + measureHeightSize);

        int width = 0;
        int height = 0;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            //测量子控件
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);

            //获取子控件的宽度和高度
            MarginLayoutParams lp = null;
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if(child.getLayoutParams() instanceof  MarginLayoutParams) {
                lp = (MarginLayoutParams) child.getLayoutParams();
                childWidth = childWidth + lp.leftMargin + lp.rightMargin;
                childHeight = childHeight + lp.topMargin + lp.bottomMargin;
            }
            //得到最大宽度 并且累加高度
            height += childHeight;
            width = Math.max(width, childWidth);
        }

        setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidthSize : width,
                (measureHeightMode == MeasureSpec.EXACTLY) ? measureHeightSize : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int top = 0;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams lp = null;
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if(child.getLayoutParams() instanceof  MarginLayoutParams) {
                lp = (MarginLayoutParams) child.getLayoutParams();
                childWidth = childWidth + lp.leftMargin + lp.rightMargin;
                childHeight = childHeight + lp.topMargin + lp.bottomMargin;
            }
            child.layout(0, top, childWidth, top + childHeight);
            top += childHeight;
        }
    }
}
